package com.cy.aircompressor.main.view;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cy.aircompressor.databinding.PageTurningIndicatorViewBinding;
import com.cy.aircompressor.main.view.pop.PageSizePopupWindow;
import com.drug.lib.util.ToastUtil;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

public class PageTurningIndicator extends FrameLayout {
    Context mContext;

    public PageTurningIndicator(@NonNull Context context) {
        this(context, null);
    }

    public PageTurningIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageTurningIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PageTurningIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
        initView();
    }

    private Integer pageNum = 1;
    private Integer pageSize = 10;
    private Integer pageTotal = 1;
    private Integer recordTotal = 1;
    private PageTurningIndicatorViewBinding mBinding;

    private void initView() {
        mBinding = PageTurningIndicatorViewBinding.inflate(LayoutInflater.from(getContext()));
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        addView(mBinding.getRoot(), layoutParams);
        setPageSize(pageSize);
        setPageNum(pageNum);
        mBinding.skipPage.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        RxView.clicks(mBinding.minusBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (pageNum == 1) return;
                    mBinding.page.setText(String.valueOf(--pageNum));
                    if (pageTurningIndicatorCallBack != null)
                        pageTurningIndicatorCallBack.skipPage(pageNum);
                });
        RxView.clicks(mBinding.addBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (pageNum >= pageTotal) return;
                    mBinding.page.setText(String.valueOf(++pageNum));
                    if (pageTurningIndicatorCallBack != null)
                        pageTurningIndicatorCallBack.skipPage(pageNum);
                });
        RxView.clicks(mBinding.skipPage)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    String s = mBinding.appointPage.getText().toString().trim();
                    if (TextUtils.isEmpty(s)) {
                        ToastUtil.show("请输入页数");
                        return;
                    }
                    int num = Integer.parseInt(s);
                    if (num > pageTotal) {
                        ToastUtil.show("没有那么多页哦");
                        return;
                    }
                    pageNum = num;
                    mBinding.page.setText(String.valueOf(pageNum));
                    if (pageTurningIndicatorCallBack != null)
                        pageTurningIndicatorCallBack.skipPage(pageNum);
                });
        RxView.clicks(mBinding.size)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    PageSizePopupWindow sizePopupWindow = new PageSizePopupWindow(mContext, mBinding.size);
                    sizePopupWindow.setDropdownCallBack(new PageSizePopupWindow.DropdownCallBack() {
                        @Override
                        public void select(Integer size) {
                            pageSize = size;
                            if (pageTurningIndicatorCallBack != null)
                                pageTurningIndicatorCallBack.pageSize(pageSize);
                        }
                    });
                    sizePopupWindow.showAsAbove(mContext, mBinding.size, sizePopupWindow);//显示PopupWindow小弹窗
                });
    }

    public void setEnableSize(boolean isEnable) {
        mBinding.size.setVisibility(isEnable ? VISIBLE : GONE);
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        mBinding.size.setText(String.format("%d条/页", pageSize));
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
        mBinding.page.setText(String.valueOf(pageNum));
    }

    public void setPageTotal(Integer pageTotal) {
        this.pageTotal = pageTotal;
        mBinding.total.setText(String.format("共计%d页", pageTotal));
    }

    public void setRecordTotal(Integer recordTotal) {
        this.recordTotal = recordTotal;
        int remainder = recordTotal % pageSize;
        int page = recordTotal / pageSize;
        pageTotal = remainder > 0 ? (page + 1) : page;
        mBinding.total.setText(String.format("共计%d页", pageTotal));
        mBinding.recordNum.setText(String.format("共%d条记录", recordTotal));
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    PageTurningIndicatorCallBack pageTurningIndicatorCallBack;

    public void setPageTurningIndicatorCallBack(PageTurningIndicatorCallBack pageTurningIndicatorCallBack) {
        this.pageTurningIndicatorCallBack = pageTurningIndicatorCallBack;
    }

    public interface PageTurningIndicatorCallBack {
        void skipPage(int page);

        void pageSize(int size);
    }
}
